home *** CD-ROM | disk | FTP | other *** search
- ;; calc.mut : a popup programmers calculator for ME
-
- (include popup.mut) ; for the doc window
- (include tobase.mut)
-
- (INT RV TV mem)
- (int base)
-
- (defun
- doc HIDDEN ; popup a window with documentation
- {
- (popup-window 0 0 40 16)
- (wputs "Mutt CALC - an RPN calculator")
- (wputs "+ - * (or x) /")
- (wputs "Enter or Return : move x to total")
- (wputs "m : Negate x")
- (wputs "| & \^ : bitwise OR, AND, XOR total and x")
- (wputs "> < : shift right or left")
- (wputs "% : total mod x")
- (wputs "s : Store total in memory")
- (wputs "r : Recall memory to x")
- (wputs "= : Insert total at dot")
- (wputs "\^H or BACKSPACE : Erase last digit of x")
- (wputs "\^L : Redraw the screen")
- (wputs "# : Toggle between decimal and hex")
- (wputs "B : Change the radix")
- (wputs "k : Put the next key pressed into total")
- (wputs 'q ^G : Quit')
- }
- num (array byte s 1) HIDDEN { (s 0) } ; convert character to number
- inc (int n) HIDDEN ; increment TV by n
- {
- (if (< n base) (TV (+ (* TV base) n)) )
- }
- MAIN { (base 10) } ; initialize base to decimal
- vert (int n) HIDDEN ; convert n to proper base for display
- {
- (if (== base 10) { n (done) } )
- (if (< n 0) (concat "-" (tobase (- 0 n) base)) (tobase n base))
- }
- odd (int n) HIDDEN { (!= n (* (/ n 2) 2)) } ; TRUE if n is odd
- bitwise (pointer defun op)(int x y) HIDDEN ; (bitwise-op x y)
- {
- (INT bit result a b)
-
- (result 0)(bit 1)(a x)(b y)
- (while (or (!= 0 a)(!= 0 b))
- {
- (if (op a b) (+= result bit))
- (*= bit 2) ; next bit
- (/= a 2)(/= b 2)
- })
- result
- }
- bor (int a b) HIDDEN { (or (odd a)(odd b)) } ;TRUE if ((a&1) OR (b&1))==1
- band (int a b) HIDDEN { (and (odd a)(odd b)) } ;TRUE if ((a&1) AND (b&1))==1
- bxor (int a b) HIDDEN { (odd (+ a b)) } ;TRUE if ((a&1) XOR (b&1))==1
- bitwise-or (int x y) HIDDEN { (bitwise (floc bor) x y) }
- bitwise-and (int x y) HIDDEN { (bitwise (floc band) x y) }
- bitwise-xor (int x y) HIDDEN { (bitwise (floc bxor) x y) }
- calculator
- {
- (int n)
-
- (while TRUE
- {
- (msg "RPN CALC>" base " Memory: " (vert mem base)
- " Total: " (vert RV) " x: " (vert TV) )
- (switch (getchar)
- "0" (inc 0)
- "1" (inc 1)
- "2" (inc 2)
- "3" (inc 3)
- "4" (inc 4)
- "5" (inc 5)
- "6" (inc 6)
- "7" (inc 7)
- "8" (inc 8)
- "9" (inc 9)
- "+" { (+= RV TV)(TV 0) }
- "-" { (-= RV TV)(TV 0) }
- "*" { (*= RV TV)(TV 0) }
- "x" { (*= RV TV)(TV 0) }
- "/" { (if (== 0 TV)(RV 0)(/= RV TV)) (TV 0) }
- "a" (inc 10)
- "b" (inc 11)
- "c" (inc 12)
- "d" (inc 13)
- "e" (inc 14)
- "f" (inc 15)
- "=" { (insert-text (vert RV))(update) }
- "^M" { (RV TV)(TV 0) } ; enter
- "m" (*= TV -1) ; change sign
- "|" { (RV (bitwise-or RV TV)) (TV 0) }
- "&" { (RV (bitwise-and RV TV)) (TV 0) }
- '^' { (RV (bitwise-xor RV TV)) (TV 0) }
- '%' { (if (== 0 TV)(RV 0)(RV (mod RV TV))) (TV 0) }
- "^H" (/= TV base)
- "s" (mem RV) ; store
- "r" (TV mem) ; recall
- "^L" { (refresh-screen)(update) } ; refresh screnn
- ">" (/= RV 2) ; shift right
- "<" (*= RV 2) ; shift left
- "#" (if (== base 10)(base 16)(base 10)) ; toggle radix
- "B" ; change radix
- {
- (n (atoi (ask "base = ")))
- (if (and (<= 2 n)(<= n 16)) (base n))
- }
- "k" { (msg "Press key to convert")(RV (num (getchar))) }
- "K" { (msg "Press ME key to convert")(RV (get-key)) }
- "?" (doc)
- "q" (break) ; quit
- "^G" (break) ; quit
- )
- })
- }
- )
-